/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Training;

import DataBase.DBConnection;
import Entities.Mail;
import Parser.EmailParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import javax.mail.MessagingException;

/**
 *
 * @author fabio
 */
public class ReadTrainingMails {

    private String trainingMailsPath = "SpamDataSet/Training/";
    private String[] trainingMailsFilenames;
    //private ArrayList mailList;

    public ReadTrainingMails() {
        //mailList = new ArrayList(5000);
    }

    public void run() throws FileNotFoundException, IOException, MessagingException {
        //Read labels
        SpamLabels rtm = new SpamLabels();
        rtm.readLabels();

        //Read all mails
        readMailsFilenames();

        for (int i = 3429; i < trainingMailsFilenames.length; i++) {
            //for (int i = 0; i < 20; i++) {

            System.out.println(i);
            Mail mail = new Mail(trainingMailsPath + trainingMailsFilenames[i], rtm.getSpamLabel(trainingMailsFilenames[i]));
            EmailParser eParser = new EmailParser();
            mail = eParser.readMail(mail);
            //mailList.add(mail);
            DBConnection.insertMail(mail);
        }
    }

    private void readMailsFilenames() {
        File dir = new File(trainingMailsPath);
        FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return name.endsWith(".eml");
            }
        };
        trainingMailsFilenames = dir.list(filter);
    }
}